package cn.buaa.beijing.base.impl;

import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import java.util.ArrayList;

import cn.buaa.beijing.MainActivity;
import cn.buaa.beijing.base.BaseMenuDetailPager;
import cn.buaa.beijing.base.BasePager;
import cn.buaa.beijing.base.menudetail.InteractMenuDetailPager;
import cn.buaa.beijing.base.menudetail.NewsMenuDetailPager;
import cn.buaa.beijing.base.menudetail.PhotoMenuDetailPager;
import cn.buaa.beijing.base.menudetail.TopicMenuDetailPager;
import cn.buaa.beijing.domain.NewsData;
import cn.buaa.beijing.fragment.LeftMenuFragment;
import cn.buaa.beijing.global.GlobalContants;


/**
 * 新闻中心
 *
 */
public class NewsCenterPager extends BasePager {
	private ArrayList<BaseMenuDetailPager> mPagers;// 4个菜单详情页的集合
	NewsData mNewsData;
	public NewsCenterPager(Activity activity) {
		super(activity);
	}

	@Override
	public void initData() {
		setSlidingMenuEnable(true);//打开侧边栏

		tvTitle.setText("新闻");

		TextView text = new TextView(mActivity);
		text.setText("新闻");
		text.setTextColor(Color.RED);
		text.setTextSize(25);
		text.setGravity(Gravity.CENTER);

		// 向FrameLayout中动态添加布局
		flContent.addView(text);
		getDataFromServer();

	}

	private void getDataFromServer() {
		HttpUtils utils = new HttpUtils(20000);
		utils.send(HttpMethod.GET, GlobalContants.CATEGORIES_URL,
				new RequestCallBack<String>() {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						Toast.makeText(mActivity, "网络访问成功", Toast.LENGTH_SHORT)
								.show();
						Log.i("result", responseInfo.result);
						parseData(responseInfo.result);
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT)
								.show();
						error.printStackTrace();
					}
				}
		);
	}

	protected void parseData(String result) {
		Gson gson = new Gson();
		mNewsData = gson.fromJson(result, NewsData.class);
		//System.out.println("解析结果:" + mNewsData);
		Log.i("result","解析结果:" + mNewsData);

		// 刷新测边栏的数据
		MainActivity mainUi = (MainActivity) mActivity;
		LeftMenuFragment leftMenuFragment = mainUi.getLeftMenuFragment();
		leftMenuFragment.setMenuData(mNewsData);

		mPagers = new ArrayList<BaseMenuDetailPager>();
		mPagers.add(new NewsMenuDetailPager(mActivity));
		mPagers.add(new TopicMenuDetailPager(mActivity));
		mPagers.add(new PhotoMenuDetailPager(mActivity));
		mPagers.add(new InteractMenuDetailPager(mActivity));

		setCurrentMenuDetailPager(0);// 设置菜单详情页-新闻为默认当前页
	}

	/**
	 * 设置当前菜单详情页
	 */
	public void setCurrentMenuDetailPager(int position) {
		BaseMenuDetailPager pager = mPagers.get(position);// 获取当前要显示的菜单详情页
		flContent.removeAllViews();// 清除之前的布局
		flContent.addView(pager.mRootView);// 将菜单详情页的布局设置给帧布局

		// 设置当前页的标题
		NewsData.NewsMenuData menuData = mNewsData.data.get(position);
		tvTitle.setText(menuData.title);

		pager.initData();// 初始化当前页面的数据
	}

}
